/*
* if-else优化
* 享元模式
* */

let houseUse = {};
house.forEach((item) => {
  if (item.name == "教室") {
    houseUse = {
      value: jsArea.toFixed(2),
      name: item.name,
      num: jsNum,
      area: jsArea.toFixed(2),
      houseTypeColor: "RGB(135,206,235)",
      houseInfo: item.houseInfo,
    };
  } else if (item.name == "图书馆") {
    houseUse = {
      value: tsgArea.toFixed(2),
      name: item.name,
      num: tsgNum,
      area: tsgArea.toFixed(2),
      houseTypeColor: "RGB(106,90,255)",
      houseInfo: item.houseInfo,
    };
  } else if (item.name == "实验实习科研") {
    houseUse = {
      value: sysxArea.toFixed(2),
      name: item.name,
      num: sysxNum,
      area: sysxArea.toFixed(2),
      houseTypeColor: "RGB(0,255,127)",
      houseInfo: item.houseInfo,
    };
  } else if (item.name == "体育馆") {
    houseUse = {
      value: tygArea.toFixed(2),
      name: item.name,
      num: tygNum,
      area: tygArea.toFixed(2),
      houseTypeColor: "RGB(216,191,216)",
      houseInfo: item.houseInfo,
    };
  } else if (item.name == "会堂") {
    houseUse = {
      value: htArea.toFixed(2),
      name: item.name,
      num: htNum,
      area: htArea.toFixed(2),
      houseTypeColor: "RGB(208,32,144)",
      houseInfo: item.houseInfo,
    };
  } else if (item.name == "行政") {
    houseUse = {
      value: xzArea.toFixed(2),
      name: item.name,
      num: xzNum,
      area: xzArea.toFixed(2),
      houseTypeColor: "RGB(255,182,193)",
      houseInfo: item.houseInfo,
    };
  } else if (item.name == "教师公寓") {
    houseUse = {
      value: jsgyArea.toFixed(2),
      num: jsgyNum,
      area: jsgyArea.toFixed(2),
      houseTypeColor: "RGB(173,255,47)",
      name: item.name,
      houseInfo: item.houseInfo,
    };
  } else if (item.name == "校外科研用房") {
    houseUse = {
      value: xwkyArea.toFixed(2),
      name: item.name,
      num: xwkyNum,
      area: xwkyArea.toFixed(2),
      houseTypeColor: "RGB(255,20,147)",
      houseInfo: item.houseInfo,
    };
  } else if (item.name == "运动场地") {
    houseUse = {
      value: ydcdArea.toFixed(2),
      name: item.name,
      num: ydcdNum,
      area: ydcdArea.toFixed(2),
      houseTypeColor: "RGB(128,128,0)",
      houseInfo: item.houseInfo,
    };
  } else if (item.name == "食堂") {
    houseUse = {
      value: stArea.toFixed(2),
      name: item.name,
      num: stNum,
      area: stArea.toFixed(2),
      houseTypeColor: "RGB(70, 130, 180)",
      houseInfo: item.houseInfo,
    };
  } else {
    houseUse = {
      value: otherArea.toFixed(2),
      name: item.name,
      num: otherlxNum,
      area: otherArea.toFixed(2),
      houseTypeColor: "RGB(70, 130, 0)",
      houseInfo: item.houseInfo,
    };
  }
});

// 优化
/*
* 1. 添加类型配置映射
* */
const houseTypeConfig = {
  "教室": {
    value: jsArea.toFixed(2),
    area: jsArea.toFixed(2),
    num: jsNum,
    houseTypeColor: "RGB(135,206,235)",
  },
  "图书馆": {
    value: tsgArea.toFixed(2),
    area: tsgArea.toFixed(2),
    num: tsgNum,
    houseTypeColor: "RGB(106,90,255)",
  },
  // 添加其他类型...
  "其他": {
    value: otherArea.toFixed(2),
    area: otherArea.toFixed(2),
    num: otherlxNum,
    houseTypeColor: "RGB(70, 130, 0)",
  },
};

house.forEach((item) => {
  const config = houseTypeConfig[item.name];
  const houseUse = {
    ...config,
    name: item.name,
    houseInfo: item.houseInfo,
  };
});

/*
* 2. switch优化if-else语句
* */
function getHouseUse(value, num, houseTypeColor) {
  return {
    value,
    num,
    area: value,
    houseTypeColor,
  };
}
house.forEach((item) => {
  let houseConfig;
  switch (item.name) {
    case "教室":
      houseConfig = getHouseUse(jsArea.toFixed(2), jsNum, "RGB(70, 130, 0)");
      break;
    case "图书馆":
      houseConfig = getHouseUse(tsgArea.toFixed(2), tsgNum, "RGB(70, 130, 0)");
      break;
    // 添加其他类型...
    default:
      houseConfig = getHouseUse(otherArea.toFixed(2), otherlxNum, "RGB(70, 130, 0)");
      break;
  }
  houseUse = {
    ...houseConfig,
    name: item.name,
    houseInfo: item.houseInfo,
  };
});

/*
* 享元模式优化
* */
var houseA = [];
var houseANum = 0.00;
var houseB = [];
var houseBNum = 0.00;
var houseC = [];
var houseCNum = 0.00;
var houseD = [];
var houseDNum = 0.00;
var houseE = [];
var houseENum = 0.00;
var houseF = [];
var houseFNum = 0.00;
data.floorInfo.fjList.forEach((item) => {
  if (item.jzmj <= 20 && item.jzmj > 0) {
    houseA.push(item);
    houseANum += Number(item.jzmj);
  }
  if (item.jzmj <= 40 && item.jzmj > 20) {
    houseB.push(item);
    houseBNum += Number(item.jzmj);
  }
  if (item.jzmj <= 60 && item.jzmj > 40) {
    houseC.push(item);
    houseCNum += Number(item.jzmj);
  }
  if (item.jzmj <= 80 && item.jzmj > 60) {
    houseD.push(item);
    houseDNum += Number(item.jzmj);
  }
  if (item.jzmj <= 100 && item.jzmj > 80) {
    houseE.push(item);
    houseENum += Number(item.jzmj);
  }
  if (item.jzmj > 100) {
    houseF.push(item);
    houseFNum += Number(item.jzmj);
  }
});
for (var i = 0; i < data.houseAeraInfo.length; i++) {
  if (data.houseAeraInfo[i].areaInterval == "0~20㎡") {
    data.houseAeraInfo[i].houseInfo = houseA;
    data.houseAeraInfo[i].num = houseA.length;
    data.houseAeraInfo[i].value = houseA.length;
    data.houseAeraInfo[i].area = houseANum.toFixed(2);
  }
  if (data.houseAeraInfo[i].areaInterval == "20~40㎡") {
    data.houseAeraInfo[i].houseInfo = houseB;
    data.houseAeraInfo[i].num = houseB.length;
    data.houseAeraInfo[i].area = houseBNum.toFixed(2);
    data.houseAeraInfo[i].value = houseB.length;
  }
  if (data.houseAeraInfo[i].areaInterval == "40~60㎡") {
    data.houseAeraInfo[i].houseInfo = houseC;
    data.houseAeraInfo[i].num = houseC.length;
    data.houseAeraInfo[i].area = houseCNum.toFixed(2);
    data.houseAeraInfo[i].value = houseC.length;
  }
  if (data.houseAeraInfo[i].areaInterval == "60~80㎡") {
    data.houseAeraInfo[i].houseInfo = houseD;
    data.houseAeraInfo[i].num = houseD.length;
    data.houseAeraInfo[i].area = houseDNum.toFixed(2);
    data.houseAeraInfo[i].value = houseD.length;
  }
  if (data.houseAeraInfo[i].areaInterval == "80~100㎡") {
    data.houseAeraInfo[i].houseInfo = houseE;
    data.houseAeraInfo[i].num = houseE.length;
    data.houseAeraInfo[i].area = houseENum.toFixed(2);
    data.houseAeraInfo[i].value = houseE.length;
  }
  if (data.houseAeraInfo[i].areaInterval == "100㎡以上") {
    data.houseAeraInfo[i].houseInfo = houseF;
    data.houseAeraInfo[i].num = houseF.length;
    data.houseAeraInfo[i].area = houseFNum.toFixed(2);
    data.houseAeraInfo[i].value = houseF.length;
  }
}


const houseNum = {
  A: {
    houses: [],
    totalNum: 0.00,
  },
  B: {
    houses: [],
    totalNum: 0.00,
  },
  C: {
    houses: [],
    totalNum: 0.00,
  },
  D: {
    houses: [],
    totalNum: 0.00,
  },
  E: {
    houses: [],
    totalNum: 0.00,
  },
  F: {
    houses: [],
    totalNum: 0.00,
  },
};
data.floorInfo.fjList.forEach((item) => {
  let type = "";
  if (item.jzmj <= 20 && item.jzmj > 0) {
    type = "A";
  } else if (item.jzmj <= 40 && item.jzmj > 20) {
    type = "B";
  } else if (item.jzmj <= 60 && item.jzmj > 40) {
    type = "C";
  } else if (item.jzmj <= 80 && item.jzmj > 60) {
    type = "D";
  } else if (item.jzmj <= 100 && item.jzmj > 80) {
    type = "E";
  } else {
    type = "F";
  }
  houseNum[type].houses.push(item);
  houseNum[type].totalNum += Number(item.jzmj);
});

for (let i = 0; i < data.houseAeraInfo.length; i++) {
  let type;
  if (data.houseAeraInfo[i].areaInterval == "0~20㎡") {
    type = "A";
  } else if (data.houseAeraInfo[i].areaInterval == "20~40㎡") {
    type = "B";
  } else if (data.houseAeraInfo[i].areaInterval == "40~60㎡") {
    type = "C";
  } else if (data.houseAeraInfo[i].areaInterval == "60~80㎡") {
    type = "D";
  } else if (data.houseAeraInfo[i].areaInterval == "80~100㎡") {
    type = "E";
  } else if (data.houseAeraInfo[i].areaInterval == "100㎡以上") {
    type = "F";
  }
  data.houseAeraInfo[i].houseInfo = houseNum[type].houses;
  data.houseAeraInfo[i].num = houseNum[type].houses.length;
  data.houseAeraInfo[i].value = houseNum[type].houses.length;
  data.houseAeraInfo[i].area = houseNum[type].totalNum.toFixed(2);
}
